<%--
  Created by IntelliJ IDEA.
  User: 李学健
  Date: 2024/3/6
  Time: 19:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--或许不写action，默认提交至本文件？--%>
<form action="" method="post" name="form">
    <input type="text" name="number1" value="">
    <select name="it">
        <option value="add">+</option>
        <option value="sub">-</option>
        <option value="mul">*</option>
        <option value="div">/</option>
    </select>
    <input type="text" name="number2" value="">
    <br>
    <input type="submit" name="submit" value="提交">
</form>
<br>
<%
    //处理乱码问题，也可以写一个方法来处理
    request.setCharacterEncoding("gb2312");
    String str1 = request.getParameter("number1");
    String str2 = request.getParameter("number2");
    String yunsuan = request.getParameter("it");

    try {
        double num1 = Double.parseDouble(str1);
        double num2 = Double.parseDouble(str2);
        //注意在这里比较是用compareTo函数比较，不能用成yunsuan==“add”
        if (yunsuan.compareTo("add") == 0) {
            out.print(num1 + "+" + num2 + "=" + (num1 + num2));
        }
        if (yunsuan.compareTo("sub") == 0) {
            out.print(num1 + "-" + num2 + "=" + (num1 - num2));
        }
        if (yunsuan.compareTo("mul") == 0) {
            out.print(num1 + "*" + num2 + "=" + (num1 * num2));
        }
        if (yunsuan.compareTo("div") == 0) {
            out.print(num1 + "/" + num2 + "=" + (num1 / num2));
        }
    } catch (Exception e) {
        out.print(e.getMessage());
        e.printStackTrace();
    }
%>
</body>
</html>
